package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tools.Tool;

import Model.Honor;

public class HonorDAO extends DAO{
	private String classname=this.getClass().getName().toString();
	public HonorDAO()
	{
		super();
	}
	public boolean  addHonor(Honor honor)
	{
		sql="insert into honor(id,name,gettime,place,shortname,smallstory,uptime,saveurl,upname,uptype) values(?,?,?,?,?,?,?,?,?,?)";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, honor.getId());
			pstmt.setString(2, honor.getName());
			pstmt.setString(3, honor.getGettime());
			pstmt.setString(4, honor.getPlace());
			pstmt.setString(5, honor.getShortname());
			pstmt.setString(6, honor.getSmallstory());
			pstmt.setString(7, honor.getUptime());
			pstmt.setString(8, honor.getSaveurl());
			pstmt.setString(9, honor.getUpname());
			pstmt.setString(10, honor.getUptype());
			if(pstmt.executeUpdate()==1)
			{
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Tool.PrintException("addhonor", "HonorDAO");
		}
		return false;
	}
	public boolean deleteHonorByHonorId(String id)
	{
		sql="delete from honor where honor.id=?";
		try {
			pstmt=conn.prepareStatement(sql);
			pstmt.setString(1, id);
			if(pstmt.executeUpdate()==1)
			{
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Tool.PrintException("addhonor", "HonorDAO");
		}
		return false;
	}
	public ArrayList<Honor> getAllHonor()
	{
		ArrayList<Honor> all=new ArrayList<Honor>();
		sql="select id,name,gettime,place,shortname,smallstory,uptime,saveurl,upname,uptype from honor";
		try {
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				Honor e=new Honor();
				e.setGettime(rs.getString("gettime"));
				e.setId(rs.getString("id"));
				e.setName(rs.getString("name"));
				e.setPlace(rs.getString("place"));
				e.setSaveurl(rs.getString("saveurl"));
				e.setShortname(rs.getString("shortname"));
				e.setSmallstory(rs.getString("smallstory"));
				e.setUpname(rs.getString("upname"));
				e.setUptime(rs.getString("uptime"));
				e.setUptype(rs.getString("uptype"));
				all.add(e);
			}
			return all;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
	}
	
	
	public static void main(String[] args)
	{
		HonorDAO honordao=new HonorDAO();
		/*Honor honor;
		for(int i=0;i<10;i++)
		{
			honor=new Honor();
			honor.setId(""+i);
			honordao.addHonor(honor);
		}*/
		
		for(Honor honor:(List<Honor>)Tool.getCurrentPage(3, 3, honordao.getAllHonor()))
		{
			System.out.println(honor);
		}
		

	}
}
